package javax.realtime;

public abstract class ReleaseParameters implements Cloneable {

	protected RelativeTime cost;
	protected RelativeTime deadline;
	protected AsyncEventHandler overrunHandler;
	protected AsyncEventHandler missHandler;
	
	protected ReleaseParameters()
	{
		this(null,null,null,null);
	}
	protected ReleaseParameters(RelativeTime cost, RelativeTime deadline, AsyncEventHandler overrunHandler, AsyncEventHandler missHandler)
	{
		setCost(cost);
		setDeadline(deadline);
		setCostOverrunHandler(overrunHandler);
		setDeadlineMissHandler(missHandler);
	}
	
	public RelativeTime getCost()
	{
		return cost;
	}
	public AsyncEventHandler getCostOverrunHandler()
	{
		return overrunHandler;
	}
	public RelativeTime getDeadline()
	{
		return deadline;
	}
	public AsyncEventHandler getDeadlineMissHandler()
	{
		return missHandler;
	}
	public void setCost(RelativeTime cost)
	{
		if(cost == null) this.cost = new RelativeTime(0,0);
		else
		{
			if((cost.getMilliseconds() < 0) || (cost.getNanoseconds() < 0)) throw new IllegalArgumentException();
			else this.cost = cost;
		}
		
	}
	public void setCostOverrunHandler(AsyncEventHandler handler)
	{
		overrunHandler = handler;
	}
	public void setDeadline(RelativeTime deadline)
	{
		if(deadline == null) throw new IllegalArgumentException();
		else if((deadline.getMilliseconds() < 0) || (deadline.getNanoseconds() < 0) || ((deadline.getMilliseconds() == 0) && (deadline.getNanoseconds() == 0))) throw new IllegalArgumentException();
		this.deadline = deadline;
	}
	public void setDeadlineMissHandler(AsyncEventHandler handler)
	{
		missHandler = handler;
	}
	public boolean setIfFeasible(RelativeTime cost, RelativeTime deadline)
	{
		if(cost == null) cost = new RelativeTime(0, 0);
		//if(deadline == null) deadline = new RelativeTime(period);
		if(cost.getMilliseconds() < 0 || cost.getNanoseconds() < 0) throw new IllegalArgumentException();
		if(deadline.getMilliseconds() < 0 || deadline.getNanoseconds() < 0 || (deadline.getMilliseconds() == 0 && deadline.getNanoseconds() == 0)) throw new IllegalArgumentException();
		setCost(cost);
		setDeadline(deadline);
		return true;
	}
}
